#!/usr/bin/python
# -*- coding: UTF-8 -*- 

import requests
import bs4 
import xlwt
import xlrd

# 读取主页的excel数据
workbook = xlrd.open_workbook(r'zol-phone.xls')
sheet = workbook.sheet_by_index(0)
cols = sheet.col_values(6)
cols_line = len(cols)
print(cols_line)

# 创建细节保存excel
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding = 'utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('Details')
# 标题 - 产品
worksheet.write(0, 0, '产品')
# 标题 - CPU
worksheet.write(0, 1, 'CPU')
# 标题 - CPU 排行
worksheet.write(0, 2, 'CPU 排行')
# 标题 - 后置
worksheet.write(0, 3, '后置')
# 标题 - 后置排行
worksheet.write(0, 4, '后置排行')
# 标题 - 前置
worksheet.write(0, 5, '前置')
# 标题 - 前置排行
worksheet.write(0, 6, '前置排行')
# 标题 - 内存
worksheet.write(0, 7, '内存')
# 标题 - 内存排行
worksheet.write(0, 8, '内存排行')
# 标题 - 电池
worksheet.write(0, 9, '电池')
# 标题 - 电池排行
worksheet.write(0, 10, '电池排行')
# 标题 - 屏幕
worksheet.write(0, 11, '屏幕')
# 标题 - 屏幕排行
worksheet.write(0, 12, '屏幕排行')
# 标题 - 分辨率
worksheet.write(0, 13, '分辨率')
# 标题 - 分辨率排行
worksheet.write(0, 14, '分辨率排行')
# 标题 - 链接
worksheet.write(0, 15, '链接')

# 爬xml
for i in range(1, cols_line):
  url = cols[i]
  kv = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 lang="cn"'}

  try:
    response = requests.get(url, headers=kv)
    response.raise_for_status()
    response.encoding = response.apparent_encoding
    # file=open("web-get-details.html"+i,"w+", encoding='utf-8')
    # file.write(response.text)
    # file.close()
    # print(response.text)
  except:
    print("web get error: "+ str(response.status_code))
    exit()

  # 打开xml
  soup = bs4.BeautifulSoup(response.text,'lxml')
  main = soup.find('div',{'class':'second-know con-1'})
  dev_list = main.find_all('div',{'class':'box-item-fl'})
  dev_line = len(dev_list)
  print("dev_line is:", dev_line)
  rank_list = main.find_all('div',{'class':'box-item-fr'})
  rank_line = len(rank_list)
  print("rank_line is:", rank_line)
  
  # 产品
  try:
    product = soup.find('title').contents[0]
    worksheet.write(i, 0, product)
    print("at",i,0,"product",product)
  except:
    print("at",i,0,"no product")
  # CPU
  try:
    cpu = dev_list[0].find(attrs={'class':'product-link'}).contents[0]
    worksheet.write(i, 1, cpu)
    print("at",i,1,"cpu",cpu)
  except:
    print("at",i,1,"no cpu")
  # CPU 排行
  try:
    cpu_rank = ''
    worksheet.write(i, 2, cpu_rank)
    print("at",i,2,"cpu_rank",cpu_rank)
  except:
    print("at",i,2,"no cpu_rank")

  # 后置
  try:
    back_camera = dev_list[1].find(attrs={'class':'product-link'}).contents[0]
    worksheet.write(i, 3, back_camera)
    print("at",i,3,"back_camera",back_camera)
  except:
    print("at",i,3,"no back_camera")
  # 后置排行
  try:
    back_camera_rank = rank_list[0].find(attrs={'class':'progress-bar'})['style']
    back_camera_rank = back_camera_rank.strip('width:')
    worksheet.write(i, 4, back_camera_rank)
    print("at",i,4,"back_camera_rank",back_camera_rank)
  except:
    print("at",i,4,"no back_camera_rank")
    
  # 前置
  try:
    front_camera = dev_list[2].find(attrs={'class':'product-link'}).contents[0]
    worksheet.write(i, 5, front_camera)
    print("at",i,5,"front_camera",front_camera)
  except:
    print("at",i,5,"no front_camera")
  # 前置排行
  try:
    front_camera_rank = rank_list[1].find(attrs={'class':'progress-bar'})['style']
    front_camera_rank = front_camera_rank.strip('width:')
    worksheet.write(i, 6, front_camera_rank)
    print("at",i,6,"front_camera_rank",front_camera_rank)
  except:
    print("at",i,6,"no front_camera_rank")

  # 内存
  try:
    memory = dev_list[3].find(attrs={'class':'product-link'}).contents[0]
    worksheet.write(i, 7, memory)
    print("at",i,7,"memory",memory)
  except:
    print("at",i,7,"no memory")
  # 内存排行
  try:
    memory_rank = rank_list[2].find(attrs={'class':'progress-bar'})['style']
    memory_rank = memory_rank.strip('width:')
    worksheet.write(i, 8, memory_rank)
    print("at",i,8,"memory_rank",memory_rank)
  except:
    print("at",i,8,"no memory_rank")
  
  # 电池
  try:
    battery = dev_list[4].find(attrs={'class':'product-link'}).contents[0]
    worksheet.write(i, 9, battery)
    print("at",i,9,"battery",battery)
  except:
    print("at",i,9,"no battery")
  # 电池排行
  try:
    battery_rank = rank_list[3].find(attrs={'class':'progress-bar'})['style']
    battery_rank = battery_rank.strip('width:')
    worksheet.write(i, 10, battery_rank)
    print("at",i,10,"battery_rank",battery_rank)
  except:
    print("at",i,10,"no battery_rank")
    
  # 屏幕
  try:
    screen = dev_list[5].find(attrs={'class':'product-link'}).contents[0]
    worksheet.write(i, 11, screen)
    print("at",i,11,"screen",screen)
  except:
    print("at",i,11,"no screen")
  # 屏幕排行
  try:
    screen_rank = rank_list[4].find(attrs={'class':'progress-bar'})['style']
    screen_rank = screen_rank.strip('width:')
    worksheet.write(i, 12, screen_rank)
    print("at",i,12,"screen_rank",screen_rank)
  except:
    print("at",i,12,"no screen_rank")
    
  # 分辨率
  try:
    resolving = dev_list[6].find(attrs={'class':'product-link'}).contents[0]
    worksheet.write(i, 13, resolving)
    print("at",i,13,"resolving",resolving)
  except:
    print("at",i,13,"no resolving")
  # 分辨率排行
  try:
    resolving_rank = rank_list[5].find(attrs={'class':'progress-bar'})['style']
    resolving_rank = resolving_rank.strip('width:')
    worksheet.write(i, 14, resolving_rank)
    print("at",i,14,"resolving_rank",resolving_rank)
  except:
    print("at",i,14,"no resolving_rank")
    
  # 链接
  worksheet.write(i, 15, url)
  print("at",i,15,"url",url)
  

# 保存
workbook.save('zol-phone-details.xls')
